package com.boot.interceptor;

import com.boot.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//HandlerInterceptor:JDK1.8开始 接口中可以定义私有方法、静态方法、default方法
public class LoginInteceptor implements HandlerInterceptor {
    /**
     * 通过返回值:true/false 是否允许通过
     *
     * 在这个类的方法中 写具体的逻辑判断
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();

        User user = (User) request.getSession().getAttribute("user");

        if(user==null){
            response.sendRedirect("/login");
            return false;
        }

        return true;
    }
}
